﻿CREATE FUNCTION [dbo].[sf_system_IsTablePartitioned]
(
	@Table NVARCHAR(256)
)
RETURNS BIT
AS
BEGIN
	DECLARE @_msparam_1 NCHAR(1), @_msparam_3 NVARCHAR(100)
	SELECT @_msparam_1 = N'2'
		, @_msparam_3=N'dbo'

	DECLARE @Value BIT 

	SELECT
	--tbl.name AS [Name]
	--, tbl.object_id AS [ID]
	--, tbl.create_date AS [CreateDate]
	--, tbl.modify_date AS [DateLastModified]
	--, ISNULL(stbl.name, N'') AS [Owner]

	--, CASE WHEN 'FG'=dsidx.type THEN dsidx.name ELSE N'' END AS [FileGroup]
	--, CASE WHEN 'PS'=dsidx.type THEN dsidx.name ELSE N'' END AS [PartitionScheme]
	@Value = CAST(CASE WHEN 'PS'=dsidx.type THEN 1 ELSE 0 END AS bit) --AS [IsPartitioned]

	FROM
	sys.tables AS tbl
	LEFT OUTER JOIN sys.database_principals AS stbl ON stbl.principal_id = ISNULL(tbl.principal_id, (OBJECTPROPERTY(tbl.object_id, 'OwnerId')))
	INNER JOIN sys.indexes AS idx ON idx.object_id = tbl.object_id and idx.index_id < @_msparam_1
	LEFT OUTER JOIN sys.data_spaces AS dstext  ON tbl.lob_data_space_id = dstext.data_space_id
	LEFT OUTER JOIN sys.change_tracking_tables AS ctt ON ctt.object_id = tbl.object_id 
	LEFT OUTER JOIN sys.data_spaces AS dsidx ON dsidx.data_space_id = idx.data_space_id
	--LEFT OUTER JOIN sys.tables AS t ON t.object_id = idx.object_id
	--LEFT OUTER JOIN sys.data_spaces AS dstbl ON dstbl.data_space_id = t.Filestream_data_space_id and idx.index_id < 2
	WHERE
	(tbl.name=@Table
	 and SCHEMA_NAME(tbl.schema_id)=@_msparam_3)


	RETURN @Value
END